package com.wxl.www.wxl.shiro.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxl.www.wxl.shiro.entity.SysRole;
import com.wxl.www.wxl.shiro.entity.UserInfo;
import com.wxl.www.wxl.shiro.service.SysRoleService;
import com.wxl.www.wxl.shiro.utils.CreateUUID;
import com.wxl.www.wxl.shiro.utils.DataUtil;
import com.wxl.www.wxl.shiro.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sys/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping("/list")
    public String list(
            @RequestParam(value="pageNo", defaultValue="1")Integer pageNo
            , @RequestParam(value="pageSize", defaultValue="10") Integer pageSize
            , Model model) {
        IPage<SysRole> page = new Page<>(pageNo, pageSize);
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
//        wrapper.orderByDesc("update_time");
        model.addAttribute("sysRoleList", sysRoleService.page(page, wrapper));
        return "sys/role/list";
    }

    @RequestMapping("/inster")
    public String toInster(){
        return "sys/role/inster";
    }

    @PostMapping("/inster")
    @ResponseBody
    public ResultUtil inster(SysRole sysRole){
        try {
            sysRole.setId(CreateUUID.getUUID32());
//            sysRole.setUpdateTime(DataUtil.getDate());
            boolean save = sysRoleService.save(sysRole);
            return new ResultUtil();
        }catch (Exception e){
            System.out.println(e);
        }
        return null;
    }

    @RequestMapping("/update")
    public String toUpdate(String id,Model model){
        model.addAttribute("sysRole",sysRoleService.getById(id));
        return "sys/role/update";
    }

    @PostMapping("/update")
    @ResponseBody
    public ResultUtil update(SysRole sysRole){
        try {
            boolean save = sysRoleService.save(sysRole);
            return new ResultUtil();
        }catch (Exception e){
            System.out.println(e);
        }
        return null;
    }

    @PostMapping("/delete")
    @ResponseBody
    public ResultUtil update(String id){
        try {
            boolean save = sysRoleService.equals(id);
            return new ResultUtil();
        }catch (Exception e){
            System.out.println(e);
        }
        return null;
    }


    @RequestMapping("/role")
    public String toRole(String id,Model model){
        model.addAttribute("sysRole",sysRoleService.getById(id));
        return "sys/role/roleIndex";
    }
    @PostMapping("/role")
    @ResponseBody
    public ResultUtil role(String[] id){
        try {
//            boolean save = sysRoleService.equals(id);
            System.out.println(id);
            return new ResultUtil();
        }catch (Exception e){
            System.out.println(e);
        }
        return null;
    }
}
